package com.aps.common.code;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @Author nbZhuozh
 * @Date 2025-10-09  09:35
 *
 * 时间序列编码策略 (产品编码: XXXX2023120100000001)
 **/
@Component
public class TimestampSequenceStrategy implements CodeGenerationStrategy {

    @Override
    public String generateCode(String prefix, CodeGenerationParameter parameter) {
        Assert.notNull(prefix, "prefix must not be null");
        Assert.notNull(parameter, "sortBy must not be null");
        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return String.format("%s%s%08d", prefix, datePart, parameter.getSequence());
    }

    @Override
    public boolean validateCode(String code) {
        return code != null && code.matches("^[A-Z]{2,}\\d{8}\\d{8}$");
    }

    @Override
    public String getCodePattern() {
        return "前缀 + 年月日(8位) + 序列号(8位)";
    }
}
